<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     <el-form-item label="学期" prop="semId">
       <el-select v-model="queryParams.semId" filterable clearable placeholder="请选择学期">
         <el-option
           v-for="item in semsterList"
           :key="item.id"
           :label="item.title"
           :value="item.id"
           size="small">
         </el-option>
       </el-select>
     </el-form-item>
    <el-form-item label="年级" prop="year">
        <el-select v-model="queryParams.year" clearable placeholder="请选择年级"   @change="getQueryClazzList">
            <el-option
            v-for="item in yearList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="专业" prop="specId">
        <el-select v-model="queryParams.specId" clearable placeholder="请选择专业"  @change="getQueryClazzList">
          <el-option
            v-for="item in specialtyList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" clearable placeholder="请选择班级">
          <el-option
            v-for="item in queryClazzList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
            size="small">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="教学形式" prop="mode">
        <el-select v-model="queryParams.mode" placeholder="请选择教学形式" clearable size="small">
          <el-option
            v-for="dict in modeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="eduTaskList">
      <el-table-column label="学期" align="center" prop="semName" />
      <el-table-column label="年级" align="center" prop="year" />
      <el-table-column label="专业" align="center" prop="specName" />
      <el-table-column label="课程" align="center" prop="courseName" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="教师" align="center" prop="teacherName" />
      <el-table-column label="教学形式" align="center" prop="mode" :formatter="modeFormat" />
      <el-table-column label="考核类型" align="center" prop="exam" :formatter="examFormat" />
      <el-table-column label="教材" align="center" prop="bookName" />
      <el-table-column label="教室" align="center" prop="roomName" />
      <el-table-column label="学分" align="center" prop="score" />
      <el-table-column label="学时" align="center" prop="periods" />
      <el-table-column label="开始周" align="center" prop="startWeek" />
      <el-table-column label="结束周" align="center" prop="endWeek" />
      <el-table-column label="确认状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['education:eduTask:check']"
          >排课</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教学任务对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
       <el-tabs type="card" >
        <el-tab-pane label="基本信息">
        <el-form-item label="课程" prop="courseId">
         <el-select v-model="form.courseId" placeholder="请输入课程" clearable filterable disabled>
           <el-option
             v-for="dict in courseList"
             :key="dict.id"
             :label="dict.title"
             :value="dict.id"
           />
         </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="classId">
          <el-select v-model="form.classId" clearable placeholder="请选择班级" disabled>
            <el-option
              v-for="item in formClazzList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教师" prop="teacherId">
         <el-select v-model="form.teacherId" placeholder="请输入老师姓名" clearable filterable disabled>
           <el-option
             v-for="dict in teacherList"
             :key="dict.userId"
             :label="dict.name"
             :value="dict.userId"
           />
         </el-select>
        </el-form-item>
        <el-form-item label="学时" prop="periods">
          <el-input v-model="form.periods" placeholder="请输入学时" disabled/>
        </el-form-item>
        <el-form-item label="开始周" prop="startWeek">
          <el-input v-model="form.startWeek" placeholder="请输入开始周" disabled />
        </el-form-item>
        <el-form-item label="结束周" prop="endWeek">
          <el-input v-model="form.endWeek" placeholder="请输入结束周" disabled />
        </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="排课设置信息">
        <el-form-item label="教室" prop="roomId">
          <el-select v-model="form.roomId" placeholder="请选择教室" filterable clearable size="small">
            <el-option
              v-for="dict in roomList"
              :key="dict.id"
              :label="dict.title"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="周课时" prop="endWeek">
          <el-input v-model="form.sections" placeholder="请输入结束周" disabled />
        </el-form-item>
        <el-form-item label="周一" prop="'week1'" v-if="inWeek(1)">
          <el-select v-model="form.week1" placeholder="请选择节次" multiple clearable size="small">
            <el-option
              v-for="dict in sectionList"
              :key="dict.id"
              :label="dict.title"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="周二" prop="'week2'" v-if="inWeek(2)">
          <el-select v-model="form.week2" placeholder="请选择节次" multiple clearable size="small">
            <el-option
              v-for="dict in sectionList"
              :key="dict.id"
              :label="dict.title"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="周三" prop="'week3'" v-if="inWeek(3)">
          <el-select v-model="form.week3" placeholder="请选择节次" multiple clearable size="small">
            <el-option
              v-for="dict in sectionList"
              :key="dict.id"
              :label="dict.title"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="周四" prop="'week4'" v-if="inWeek(4)">
          <el-select v-model="form.week4" placeholder="请选择节次" multiple clearable size="small">
            <el-option
              v-for="dict in sectionList"
              :key="dict.id"
              :label="dict.title"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="周五" prop="'week5'" v-if="inWeek(5)">
          <el-select v-model="form.week5" placeholder="请选择节次" multiple clearable size="small">
            <el-option
              v-for="dict in sectionList"
              :key="dict.id"
              :label="dict.title"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="周六" prop="'week6'" v-if="inWeek(6)">
          <el-select v-model="form.week6" placeholder="请选择节次" multiple clearable size="small">
            <el-option
              v-for="dict in sectionList"
              :key="dict.id"
              :label="dict.title"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="周日" prop="'week7'" v-if="inWeek(7)">
          <el-select v-model="form.week7" placeholder="请选择节次" multiple clearable size="small">
            <el-option
              v-for="dict in sectionList"
              :key="dict.id"
              :label="dict.title"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEduTask, getEduTask, delEduTask, addEduTask, updateEduTask, exportEduTask } from "@/api/education/eduTask";
import { manualScheduling} from "@/api/education/eduScheduling";

import { selectRoom } from "@/api/base/room";
import { getToken } from "@/utils/auth";
import { selectSpecialty} from "@/api/base/specialty";
import { selectSemster} from "@/api/base/semster";
import { selectClazz } from "@/api/base/clazz";
import { selectCourse} from "@/api/base/course";
import { selectTeacher} from "@/api/base/teacher";
import { selectSection} from "@/api/base/section";

export default {
  name: "SchedulingTask",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学期数据
      semsterList: [],
      //起始年
      yearList:[],
      // 专业表格数据
      specialtyList: [],
      // 班级表格数据
      formClazzList: [],
      // 班级表格数据
      queryClazzList: [],
      // 教师表格数据
      teacherList: [],
      // 课程数据
      courseList: [],
      // 房间数据
      roomList: [],
      // 节次数据
      sectionList: [],
      // 教学任务表格数据
      eduTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 教学形式字典
      modeOptions: [],
      // 考核类型字典
      examOptions: [],
      // 确认状态字典
      statusOptions: [],
      // 周状态字典
      weekOptions: [],
      weeks:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        semId: null,
        specId: null,
        courseId: null,
        mode: null,
        classId: null,
        teacherId: null,
        exam: null,
        result: null,
        bookId: null,
        roomId: null,
        score: null,
        periods: null,
        startWeek: null,
        endWeek: null,
        addTime: null,
        addUser: null,
        updateUser: null,
        status: 'Y'
      },
      resultSet:'',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: "年级不能为空", trigger: "blur" }
        ],
        semId: [
          { required: true, message: "学期不能为空", trigger: "blur" }
        ],
        specId: [
          { required: true, message: "专业不能为空", trigger: "blur" }
        ],
        courseId: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ],
        classId: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "教师不能为空", trigger: "blur" }
        ],
        bookId: [
          { required: true, message: "教材不能为空", trigger: "blur" }
        ],
        roomId: [
          { required: true, message: "教室不能为空", trigger: "blur" }
        ],
        periods: [
          { required: true, message: "学时不能为空", trigger: "blur" }
        ],
        startWeek: [
          { required: true, message: "开始周不能为空", trigger: "blur" }
        ],
        endWeek: [
          { required: true, message: "结束周不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("education_mode").then(response => {
      this.modeOptions = response.data;
    });
    this.getDicts("exam_type").then(response => {
      this.examOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("common_week").then(response => {
      this.weekOptions = response.data;
    });
    this.getConfigKey("edu.scheduling.week").then(response => {
      this.weeks = response.msg;
    });

    this.getConfigKey("sys.time.start").then(response => {
      var st = parseInt(response.msg);
      var dt = new Date();
      while(st<=dt.getFullYear()+1){
        this.yearList.push({value: st,label: st});
        st ++;
      }
    });
    this.getConfigKey("edu.result.set").then(response => {
      this.resultSet = response.msg;
    });
    selectSpecialty({'status':1}).then(response => {
      this.specialtyList = response.rows;
    });
    selectSemster({'status':1}).then(response => {
      this.semsterList = response.rows;
    });
    selectCourse().then(response => {
        this.courseList = response.rows;
        this.total = response.total;
      });
    selectTeacher({'status':1}).then(response => {
        this.teacherList = response.rows;
        this.total = response.total;
      });
    selectSection({'status':1}).then(response => {
        this.sectionList = response.rows;
        this.total = response.total;
      });
    selectRoom({'status':1, type : '1'}).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
      });
  },
  methods: {
    /** 查询教学任务列表 */
    getList() {
      this.loading = true;
      listEduTask(this.queryParams).then(response => {
        this.eduTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     /** 查询班级列表 */
    getFormClazzList() {
      this.loading = true;
      var p = {status:'1'};
      if(this.form.year) p.year = this.form.year;
      if(this.form.specId) p.specId = this.form.specId;
      selectClazz(p).then(response => {
        this.formClazzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     /** 查询班级列表 */
    getQueryClazzList() {
      this.loading = true;
      var p = {status:'1'};
      if(this.queryParams.year) p.year = this.queryParams.year;
      if(this.queryParams.specId) p.specId = this.queryParams.specId;
      selectClazz(p).then(response => {
        this.queryClazzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 教学形式字典翻译
    modeFormat(row, column) {
      return this.selectDictLabel(this.modeOptions, row.mode);
    },
    // 考核类型字典翻译
    examFormat(row, column) {
      return this.selectDictLabel(this.examOptions, row.exam);
    },
    // 确认状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    inWeek(w) {
      if(this.weeks.indexOf(w) >= 0) return true;
      return false;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        semId: null,
        specId: null,
        courseId: null,
        mode: null,
        classId: null,
        teacherId: null,
        exam: "1",
        result: null,
        bookId: null,
        roomId: null,
        score: null,
        periods: null,
        startWeek: null,
        endWeek: null,
        addTime: null,
        addUser: null,
        updateTime: null,
        updateUser: null,
        status: "N"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教学任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form.taskId = id;
      getEduTask(id).then(response => {
        this.form = response.data;
        this.form.taskId = id;
        this.form.sections = Math.ceil(this.form.periods/(this.form.endWeek - this.form.startWeek +1));
        this.getFormClazzList();
        this.open = true;
        this.title = "排课设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.id = null;
          manualScheduling(this.form).then(response => {
            this.msgSuccess("排课成功");
            this.open = false;
            // this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除教学任务编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEduTask(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教学任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEduTask(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
